{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 字典(Dictionary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3}\n",
      "2\n",
      "{'a': 2, 'b': 6, 'd': 9}\n",
      "10\n",
      "{'a': 10, 'd': 9}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-1c20fb98dfb8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     31\u001b[0m \u001b[1;31m# 键必须不可变，所以可以用数字，字符串或元组充当，所以用列表就不行，如下实例：\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     32\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 33\u001b[1;33m \u001b[0mdict3\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'key'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m123\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'zk'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     34\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdict3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#  报错 unhashable type: 'list'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "'''\n",
    "\n",
    "字典是另一种可变容器模型，且可存储任意类型对象。\n",
    "\n",
    "字典的每个键值 key=>value 对用冒号 : 分割，每个键值对之间用逗号 , 分割，整个字典包括在花括号 {} 中 ,格式如下所示：\n",
    "\n",
    "'''\n",
    "\n",
    "dict1 = {'a':1,'b':2,'c':3}\n",
    "print(dict1)\n",
    "print(dict['b'])\n",
    "\n",
    "# 键一般是唯一的，如果重复最后的一个键值对会替换前面的，值不需要唯一。\n",
    "dict2 = {'a':2,'b':6,'d':7,'d':9}\n",
    "\n",
    "print(dict2)\n",
    "\n",
    "\n",
    "# 修改字典\n",
    "dict2['a'] = 10\n",
    "print(dict2['a'])\n",
    "\n",
    "\n",
    "# 删除字典元素\n",
    "\n",
    "del dict2['b']\n",
    "print(dict2)\n",
    "dict2.clear()  # 清空字典所有条目\n",
    "\n",
    "\n",
    "# 键必须不可变，所以可以用数字，字符串或元组充当，所以用列表就不行，如下实例：\n",
    "\n",
    "dict3 = {['key']:123,'zk': 0}\n",
    "print(dict3)#  报错 unhashable type: 'list'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python字典包含了以下内置方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3}\n",
      "{'android': 10, 'java': 10, 'php': 10}\n",
      "10\n",
      "10\n",
      "True\n",
      "dict_items([('android', '安卓'), ('java', 'java编程'), ('AI', '人工智能')])\n",
      "android ---- 安卓\n",
      "java ---- java编程\n",
      "AI ---- 人工智能\n",
      "{'android': '安卓', 'java': 'java编程', 'AI': '人工智能', 'asp': '.net编程'}\n",
      "{'shell': '脚本', 'adb': 'android 命令'}\n",
      "{'shell': '脚本2', 'adb': 'android 命令'}\n",
      "dict9.values :  dict_values(['脚本2', 'android 命令'])\n",
      "python\n"
     ]
    }
   ],
   "source": [
    "# copy() 函数返回一个字典的浅复制\n",
    "\n",
    "dict4 = dict1.copy() \n",
    "print(dict4)\n",
    "\n",
    "\n",
    "\n",
    "# Python 字典(Dictionary) fromkeys()方法\n",
    "\n",
    "'''\n",
    "\n",
    "参数\n",
    "seq -- 字典键值列表。\n",
    "value -- 可选参数, 设置键序列（seq）的值。\n",
    "\n",
    "'''\n",
    "\n",
    "seq = ('android','java','php')\n",
    "dict6 = dict.fromkeys(seq)\n",
    "dict6 = dict.fromkeys(seq,10)\n",
    "print(dict6)\n",
    "\n",
    "\n",
    "# get() 函数返回指定键的值，如果值不在字典中返回默认值。\n",
    "print(dict6.get('android'))\n",
    "print(dict6.get('android',20))\n",
    "\n",
    "\n",
    "# has_key()方法 函数用于判断键是否存在于字典中，如果键在字典dict里返回true，否则返回false。\n",
    "\n",
    "\n",
    "# print(dict6.has_key('android'))# 报错信息 'dict' object has no attribute 'has_key'\n",
    "\n",
    "#  3.X 里不包含 has_key() 函数，被 __contains__(key) 替代:\n",
    "\n",
    "print(dict6.__contains__('android'))# True\n",
    "\n",
    "\n",
    "\n",
    "# items() 函数以列表返回可遍历的(键, 值) 元组数组。\n",
    "dict7 = {'android':'安卓','java':'java编程','AI':'人工智能'}\n",
    "print(dict7.items())\n",
    "\n",
    "for key,values in dict7.items():\n",
    "    print(key,'----',values)\n",
    "    \n",
    "    \n",
    "    \n",
    "\n",
    "# setdefault() 函数和 get()方法 类似, 如果键不存在于字典中，将会添加键并将值设为默认值。\n",
    "'''\n",
    "\n",
    "参数\n",
    "key -- 查找的键值。\n",
    "default -- 键不存在时，设置的默认键值。\n",
    "返回值\n",
    "如果字典中包含有给定键，则返回该键对应的值，否则返回为该键设置的值。\n",
    "\n",
    "setdefault 同时也会改变 dict 的值:\n",
    "\n",
    "'''\n",
    "\n",
    "dict7.setdefault('asp','.net编程')\n",
    "print(dict7)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# update() 函数把字典dict8的键/值对更新到dict9里。\n",
    "\n",
    "\n",
    "\n",
    "dict8 = {'adb':'android 命令'}\n",
    "dict9 = {'shell':'脚本'}\n",
    "\n",
    "dict10 = {'shell':'脚本2'}\n",
    "dict9.update(dict8)\n",
    "print(dict9)\n",
    "dict9.update(dict10)# 有相同的键会直接替换成 update 的值:\n",
    "print(dict9)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# values() 函数以列表返回字典中的所有值。\n",
    "\n",
    "print('dict9.values : ',dict9.values())\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# pop() 方法删除字典给定键 key 及对应的值，返回值为被删除的值。key 值必须给出。 否则，返回 default 值。\n",
    "\n",
    "'''\n",
    "\n",
    "pop()方法语法：\n",
    "\n",
    "pop(key[,default])\n",
    "参数\n",
    "key: 要删除的键值\n",
    "default: 如果没有 key，返回 default 值\n",
    "返回值\n",
    "返回被删除的值。\n",
    "\n",
    "del 语句和 pop() 函数作用相同，pop() 函数有返回值。\n",
    "\n",
    "'''\n",
    "\n",
    "site= {'name': 'python', 'alexa': 10000, 'url': 'www.runoob.com'}\n",
    "pop_obj=site.pop('name')\n",
    "print (pop_obj)    # 输出 ：python\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
